/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 28.07.2008
*/

package it.cums.wizards;

import it.cums.model.Invoice;
import it.cums.model.Order;
import it.cums.util.Converter;

import org.eclipse.jface.wizard.Wizard;

public class InvoiceWizard extends Wizard {
	private InvoiceWizardPage invoiceWizardPage;
	private Order order;
	private Invoice invoice;
	
	public InvoiceWizard(Order order) {
		super();
		this.order = order;
		setNeedsProgressMonitor(true);
		setWindowTitle("Add invoice");
	}
	
	public InvoiceWizard(Invoice invoice) {
		super();
		this.invoice = invoice;
		setNeedsProgressMonitor(true);
		setWindowTitle("Edit invoice");
	}

	@Override
	public void addPages() {
		if (invoice == null) {
			invoiceWizardPage = new InvoiceWizardPage();
		} else {
			invoiceWizardPage = new InvoiceWizardPage(invoice);
		}
		addPage(invoiceWizardPage);
	}
	
	@Override
	public boolean performFinish() {
		if (invoice == null) {
			invoice = new Invoice();
			try {
				invoice.setDueDate(Converter.convertToDate(invoiceWizardPage.getDueDate()));
				invoice.setPaymentDate(Converter.convertToDate(invoiceWizardPage.getPaymentDate()));
			} catch (Exception e) {
				
			}
			invoice.setAmount(Converter.convertToDouble(invoiceWizardPage.getAmount()));
			invoice.setCondition(invoiceWizardPage.getCondition());
			invoice.setType(invoiceWizardPage.getType());
			invoice.setStatus(invoiceWizardPage.getStatus());
			order.getInvoices().addInvoices(invoice);
			invoice.setOrder(order);
		} else {
			try {
				invoice.setDueDate(Converter.convertToDate(invoiceWizardPage.getDueDate()));
				invoice.setPaymentDate(Converter.convertToDate(invoiceWizardPage.getPaymentDate()));
			} catch (Exception e) {
				
			}
			invoice.setAmount(Converter.convertToDouble(invoiceWizardPage.getAmount()));
			invoice.setCondition(invoiceWizardPage.getCondition());
			invoice.setType(invoiceWizardPage.getType());
			invoice.setStatus(invoiceWizardPage.getStatus());
		}
		return true;
	}
}
